package com.android.common.base.rv;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.android.common.constant.Constant;
import java.util.List;

public abstract class BaseRvAdapter<T> extends RecyclerView.Adapter<BaseRvHolder<T> > {

    public static final int TYPE_HEADER = 100;
    public static final int TYPE_FOOTER = TYPE_HEADER + 1;
    public static final int TYPE_BODY = TYPE_FOOTER + 1;

    private List<T> mTs;

    protected Context mContext;

    @NonNull
    @Override
    public BaseRvHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_HEADER:
            case TYPE_BODY:
            case TYPE_FOOTER:
                return createDataViewHolder(parent, viewType);
        }
        return createDataViewHolder(parent, viewType);

    }

    protected View inflate(int layoutId, ViewGroup parent) {
        return LayoutInflater.from(mContext).inflate(layoutId, parent, false);
    }

    public BaseRvAdapter(Context context) {
        mContext = context;
    }

    public BaseRvAdapter(Context context, List<T> ts) {
        mContext = context;
        mTs = ts;
    }

    public void setData(List<T> ts){
        if (ts != null) {
            mTs = ts ;
        }
    }

    public void setDataAndNotify(List<T> ts) {
        if (ts != null) {
            mTs = ts ;
            notifyDataSetChanged();
        }
    }

    public void appendDataAndNotify(List<T> ts) {
        if ((mTs != null) && (ts != null)) {
            mTs.addAll(ts);
            notifyDataSetChanged();
        }

    }

    public void appendDataAndNotify(int index, List<T> ts) {
        if ((mTs != null) && (ts != null)) {
            mTs.addAll(index,ts);
            notifyItemInserted(index);
        }
    }

    public void remove(int position){
        if ((mTs != null) && (position >= 0) && (position < mTs.size())) {
            mTs.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void updateItem(int position){
        if ((mTs != null) && (position >= 0) && (position < mTs.size())) {
            notifyItemChanged(position);
        }
    }

    public void clear() {
        if (mTs != null){
            mTs.clear();
            notifyDataSetChanged();
        }
    }

    protected abstract BaseRvHolder<T> createDataViewHolder(ViewGroup parent, int viewType);

    @Override
    public int getItemViewType(int position) {
        return getMultiplyItemViewType(position);
    }

    /**
     * 多个item类型复写此方法
     * @param position position
     * @return type
     */
    public int getMultiplyItemViewType(int position) {
        return TYPE_BODY;
    }


    @Override
    public void onBindViewHolder(final BaseRvHolder<T> holder, int position) {
        holder.toBindData(getItem(holder.getBindingAdapterPosition()), holder.getBindingAdapterPosition());
        // 点击事件的处理
        initClickListener(holder);
    }

    @Nullable
    public T getItem(int position) {
        if ((mTs != null) && (position >= 0) && (position < mTs.size())) {
            return mTs.get(position);
        }
        return null;
    }

    /**
     * 返回List<T>可能为空
     * @return List<T>
     */
    @Nullable
    public List<T>  getData(){
        return mTs;
    }

    /**
     * 条目的点击和长按事件
     * @param holder viewHolder
     */
    public void initClickListener(final RecyclerView.ViewHolder holder) {
        if (mOnItemClickListener != null && (holder.getItemViewType() != TYPE_HEADER)) {
            holder.itemView.setOnClickListener(v ->
                mOnItemClickListener.onItemClick(holder.itemView, holder.getBindingAdapterPosition()));
        }

        if (mOnItemLongClickListener != null && (holder.getItemViewType() != TYPE_HEADER)) {
            holder.itemView.setOnLongClickListener(v -> {
                mOnItemLongClickListener.onItemLongClick(holder.itemView, holder.getBindingAdapterPosition());
                return true;
            });
        }
    }


    @Override
    public int getItemCount() {
        if (mTs != null){
            return mTs.size();
        }else {
           return Constant.ZERO;
        }
    }

    protected int getTsSize() {
        if (mTs != null){
            return mTs.size();
        }else {
            return Constant.ZERO;
        }
    }


    /**
     * 单个条目的点击事件
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 单个条目的长按事件
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    private OnItemLongClickListener mOnItemLongClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }
}
